<?php
/**
 * Distribution.php
 * Niushop商城系统 - 团队十年电商经验汇集巨献!
 * =========================================================
 * Copy right 2015-2025 山西牛酷信息科技有限公司, 保留所有权利。
 * ----------------------------------------------
 * 官方网址: http://www.niushop.com.cn
 * 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和使用。
 * 任何企业和个人不允许对程序代码以任何形式任何目的再发布。
 * =========================================================
 * @author : niuteam
 * @date : 2015.1.17
 * @version : v1.0.0.0
 */

namespace addons\Nsfx\admin\controller;

use addons\Nsfx\data\service\NfxRegionAgentApply;
use app\admin\controller\BaseController;

/**
 * 区域代理控制器
 */
class Regionagent extends BaseController
{
    public $addon_view_path;

    public function __construct()
    {
        parent::__construct();
        $this->addon_view_path = ADDON_DIR . '/Nsfx/template/';
    }

    /**
     * 申请审核列表
     */
    public function applyCheckList()
    {
        if(request()->isAjax()){
            $page_index = input('page_index', 1);
            $page_size = input('page_size', PAGESIZE);
            $search_text = input('search_text', '');
            $audit_status = input('audit_status', '');

            $condition = [];
            if($search_text != ''){
                $condition['user.user_tel|user.nick_name'] = ['like', '%'. $search_text .'%'];
            }
            if($audit_status != ''){
                $condition['audit_status'] = $audit_status;
            }
            $order = 'apply.apply_time desc';

            $region_agent_apply = new NfxRegionAgentApply();
            $res = $region_agent_apply->getApplyPageList($page_index, $page_size, $condition, $order);
            $audit_status = $region_agent_apply->getApplyStatus();
            foreach($res['data'] as $key=>$val){
                $res['data'][$key]['address'] = $val['address_arr']['province_name'] . $val['address_arr']['city_name'] . $val['address_arr']['district_name'];
                $res['data'][$key]['action'] = $audit_status[$val['audit_status']]['check_action'];
            }
            return $res;
        }else{
            //审核状态
            $region_agent_apply = new NfxRegionAgentApply();
            $audit_status = $region_agent_apply->getApplyStatus();
            $this->assign('audit_status', $audit_status);

            return view($this->addon_view_path . $this->style . "Regionagent/applyCheckList.html");
        }
    }

    /**
     * 申请审核详情
     */
    public function applyCheckDetail()
    {
        $apply_id = input('apply_id');
        $region_agent_apply = new NfxRegionAgentApply();

        //当前申请
        $apply_info = $region_agent_apply->getApplyInfo(['apply_id' => $apply_id], '*');
        $this->assign('apply_info', $apply_info);

        //历史申请
        $apply_list = $region_agent_apply->getApplyPageList(1, 0, ['apply.uid' => $apply_info['uid'], 'apply_id' => ['neq', $apply_id]], 'apply_time desc');
        $this->assign('apply_list', $apply_list['data']);


        //当前申请操作记录
        $action_list = $region_agent_apply->getApplyActionList($apply_id);
        $this->assign('action_list', $action_list);

        return view($this->addon_view_path . $this->style . "Regionagent/applyCheckDetail.html");
    }

    /**
     * 申请审核通过
     */
    public function checkPass()
    {
        if(request()->isAjax()){
            $apply_id = input('apply_id', 0);
            $data = [
                'action_uid' => $this->uid,
                'give_money' => input('give_money', 0.00)
            ];
            $region_agent_apply = new NfxRegionAgentApply();
            return $region_agent_apply->applyCheckPass($apply_id, $data);
        }
    }

    /**
     * 申请审核拒绝
     */
    public function checkRefuse()
    {
        if(request()->isAjax()){
            $apply_id = input('apply_id', 0);
            $data = [
                'action_uid' => $this->uid,
                'remark' => input('remark', '')
            ];
            $region_agent_apply = new NfxRegionAgentApply();
            return $region_agent_apply->applyCheckRefuse($apply_id, $data);
        }
    }

    /**
     * 申请审核列表
     */
    public function applyConfirmList()
    {
        if(request()->isAjax()){
            $page_index = input('page_index', 1);
            $page_size = input('page_size', PAGESIZE);
            $search_text = input('search_text', '');
            $audit_status = input('audit_status', '');

            $condition = [];
            if($search_text != ''){
                $condition['user.user_tel|user.nick_name'] = ['like', '%'. $search_text .'%'];
            }
            if($audit_status != ''){
                $condition['audit_status'] = $audit_status;
            }
            $order = 'apply.apply_time desc';

            $region_agent_apply = new NfxRegionAgentApply();
            $res = $region_agent_apply->getApplyPageList($page_index, $page_size, $condition, $order);
            $audit_status = $region_agent_apply->getApplyStatus();
            foreach($res['data'] as $key=>$val){
                $res['data'][$key]['address'] = $val['address_arr']['province_name'] . $val['address_arr']['city_name'] . $val['address_arr']['district_name'];
                $res['data'][$key]['action'] = $audit_status[$val['audit_status']]['check_action'];
            }
            return $res;
        }else{
            //审核状态
            $region_agent_apply = new NfxRegionAgentApply();
            $audit_status = $region_agent_apply->getApplyStatus();
            $this->assign('audit_status', $audit_status);

            return view($this->addon_view_path . $this->style . "Regionagent/applyConfirmList.html");
        }
    }

    /**
     * 申请确认列表
     */
    public function applyConfirmDetail()
    {
        $apply_id = input('apply_id');
        $region_agent_apply = new NfxRegionAgentApply();

        //当前申请
        $apply_info = $region_agent_apply->getApplyInfo(['apply_id' => $apply_id], '*');
        $this->assign('apply_info', $apply_info);

        //历史申请
        $apply_list = $region_agent_apply->getApplyPageList(1, 0, ['apply.uid' => $apply_info['uid'], 'apply_id' => ['neq', $apply_id]], 'apply_time desc');
        $this->assign('apply_list', $apply_list['data']);

        //当前申请操作记录
        $action_list = $region_agent_apply->getApplyActionList($apply_id);
        $this->assign('action_list', $action_list);

        return view($this->addon_view_path . $this->style . "Regionagent/applyConfirmDetail.html");
    }

    /**
     * 申请确认通过
     */
    public function confirmPass()
    {
        if(request()->isAjax()){
            $apply_id = input('apply_id', 0);
            $data = [
                'action_uid' => $this->uid,
            ];
            $region_agent_apply = new NfxRegionAgentApply();
            return $region_agent_apply->applyConfirmPass($apply_id, $data);
        }
    }

    /**
     * 申请确认驳回
     */
    public function confirmRefuse()
    {
        if(request()->isAjax()){
            $apply_id = input('apply_id', 0);
            $data = [
                'action_uid' => $this->uid,
                'remark' => input('remark', '')
            ];
            $region_agent_apply = new NfxRegionAgentApply();
            return $region_agent_apply->applyConfirmRefuse($apply_id, $data);
        }
    }

    public function test()
    {
        /*$region_agent_apply = new NfxRegionAgentApply();
        $data = [
            'uid' => 7,
            'province_id' => 4,
            'city_id' => 14,
            'district_id' => 209,
            'category_ids' => "7,8,9,10,11,12",
        ];
        $res = $region_agent_apply->addApply($data);
        dump($res);*/

        /*$region_agent_apply = new NfxRegionAgentApply();
        $apply_info = $region_agent_apply->getApplyInfo(['apply_id' => 7]);
        //发放赠送金额
        $member_account = new \data\service\Member\MemberAccount();
        $res = $member_account->addMemberAccountData(0, 4, $apply_info['uid'], 1, $apply_info['give_money'], 21, 7, '申请服务中心通过，平台赠送金额');
        dump($res);exit;*/
    }
}
	
